/*
Copyright 2007 Nathan Baker

This file is part of XAliens.

XAliens is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

XAliens is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with XAliens.  If not, see <http://www.gnu.org/licenses/>.
 */

package sprites;

import java.awt.*;
import java.awt.image.BufferedImage;

public class BackgroundScroller extends Sprite {

    private int t = 0;
    private int parentWidth = 0;
    private int parentHeight = 0;

    /**
     * Creates a BackgroundScroller using an image.
     *
     * @param img The image to load from.
     */
    public BackgroundScroller(BufferedImage img) {
        mImage = img;
        mImageH = mImage.getHeight(null);
        mImageW = mImage.getWidth(null);
    }

    /**
     * Draws the next frame.
     *
     * @param g The graphics context to draw to.
     */
    public void draw(Graphics2D g) {
        g.setPaint(new TexturePaint((BufferedImage) mImage, new Rectangle(0, t,
                getWidth(), getHeight())));
        g.fillRect(0, 0, getParentWidth(), getParentHeight());
    }

    /**
     * Increments the background by the specified amount. Positive int creates a
     * downward scrolling background and vice-versa.
     *
     * @param amount The amount to scroll the background by in pixels.
     */
    public void increment(int amount) {
        t += amount;
    }

    /**
     * Gets the width of the containing frame.
     *
     * @return The width of the containing frame.
     */
    public int getParentWidth() {
        return parentWidth;
    }

    /**
     * Gets the height of the containing frame.
     *
     * @return The height of the containing frame.
     */
    public int getParentHeight() {
        return parentHeight;
    }

    /**
     * Sets the width of the containing frame.
     *
     * @param w The width of the containing frame.
     */
    public void setParentWidth(int w) {
        parentWidth = w;
    }

    /**
     * Sets the height of the containing frame.
     *
     * @param h The height of the containing frame.
     */
    public void setParentHeight(int h) {
        parentHeight = h;
    }
}
